package com.market.helper;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.market.base.utils.WebUtils;
import com.market.utils.DateFormatUtil;
import com.market.utils.LongCodeUtils;

/**
 * Created with IntelliJ IDEA.
 * Author: GuSi
 * Create: GuSi (14-7-23 21:49)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
public class AttachHelper {

	public static String saveAttach(HttpServletRequest request,MultipartFile attachFile,String pathFile,boolean normal) throws IOException {
        String path = "";
        if(normal) {
            path  = WebUtils.getServerPath(request) + pathFile;
        }else {
            path = pathFile;
        }
		File file = new File(path);
		//不存在文件目录则创建
		if(!file.exists()){
			file.mkdirs();
		}
		
		String originaFileName =  attachFile.getOriginalFilename();
		String extName = originaFileName.substring(originaFileName.lastIndexOf("."));
		String realAttachName = "88" + extName;
		
		BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(path+ File.separator+realAttachName));
		bo.write(attachFile.getBytes());
		bo.flush();
		bo.close();
        if(normal) {
            return (File.separator + pathFile+ File.separator+realAttachName).replace(File.separator,"/");
        }else {
            return (pathFile+ File.separator+realAttachName).replace(File.separator,"/");
        }

	}
}
